#include "image_layer.h"
#include "utility.h"
#include "unchecked.xpm"
#include "checked.xpm"
#include <wx/icon.h>
#include <wx/imaglist.h>

ImageLayer::ImageLayer(wxWindow *parent, const wxWindowID id,const wxPoint& pos, const wxSize& size):
	wxTreeCtrl(parent, id, pos, size, wxTR_HIDE_ROOT)
{
	Bind(wxEVT_COMMAND_TREE_SEL_CHANGED,&ImageLayer::OnSelChanged,this);
	Bind(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK,&ImageLayer::OnItemStateClick,this,id);

	wxIcon icons[2];
    icons[0] = wxIcon(unchecked);
    icons[1] = wxIcon(checked);
    int width  = icons[0].GetWidth();
    int height = icons[0].GetHeight();
    wxImageList *states = new wxImageList(width, height, true);
	states->Add(icons[0]);
    states->Add(icons[1]);
    AssignStateImageList(states);
	AddRoot("root");
}

void ImageLayer::OnSelChanged(wxTreeEvent& event)
{
	wxTreeItemId  item = event.GetItem();
	MyTreeItemData<SLayer*> *pItemData = (MyTreeItemData<SLayer*>*)GetItemData(item);
	SLayer *pLayer = pItemData->GetData();
	SetSelAction(pLayer);
	//RefreshAction();
}
void ImageLayer::OnItemStateClick(wxTreeEvent& event)
{
	wxTreeItemId  item = event.GetItem();
	MyTreeItemData<SLayer*> *pItemData = (MyTreeItemData<SLayer*>*)GetItemData(item);
	SLayer *pLayer = pItemData->GetData();
	pLayer->isShow = !pLayer->isShow;
	int state = GetItemState(item);
	if(state == 0)
		state = 1;
	else 
		state = 0;
    SetItemState(item, state);
	RefreshAction();
}

void ImageLayer::AddLayer(SLayer *pLayer)
{
	MyTreeItemData<SLayer*> *pItemData = new MyTreeItemData<SLayer*>;
	pItemData->SetData(pLayer);
	wxTreeItemId item = AppendItem(GetRootItem(),pLayer->image.name,-1,-1,pItemData);
	SetFocusedItem(item);
	if(pLayer->isShow)
		SetItemState(item,1);
	else
		SetItemState(item,0);
	Refresh();
}
void ImageLayer::DelLayer(SLayer *pLayer)
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemIdValue cookie;
	wxTreeItemId item = GetFirstChild(root,cookie);
	while(item.IsOk())
	{
		MyTreeItemData<SLayer*> *pItemData = (MyTreeItemData<SLayer*>*)GetItemData(item);
		if(pItemData->GetData() == pLayer)
		{
			Delete(item);
			return;
		}
		item = GetNextChild(root,cookie);
	}
}
